﻿/**
*Main Page class
*auth:lww
*version:1.0
*since:2014-10-11
*@param:context:Application
*@param:router:Router
*/
function MainPage(context,router) {
    this.id = "";
    this.name = "MainPage";
    this.version = "1.0.0";
    this.mContext = context;
    this.mRootViewObject = null;
    this.mDialogView = null;
    this.mRouter = router;  
    this.mainNavView = null;   
    this.menuLayer = null;     
    this.dataLayer = null;
    this.currentPageFrame = null; 
    this.pageFrames = new Dictionary("openPageFrames"); 
}
MainPage.prototype.getId = function () {
    if (this.id == null || this.id == "") {
        this.id = id();
    }
    return this.id;
};
MainPage.prototype.setId = function (ids) {
    this.id = ids;
};
MainPage.prototype.invalidate = function () {
    if (this.mRootViewObject == null) {
        this.mRootViewObject = new LinearLayout(this.mContext);
        this.mRootViewObject.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT));
        this.mRootViewObject.setBackground(new Drawable(null, DefaultBgImage));
        this.mRootViewObject.view().style.minWidth = "200px";
        this.mRootViewObject.view().style.minHeight = "100px";
        this.mRootViewObject.view().style.overflow = "hidden";
    }
    return this.mRootViewObject.view();
};
MainPage.prototype.view = function () {
    return this.invalidate();
};
MainPage.prototype.initPage = function () {  
    this.initPageView();
};
MainPage.prototype.initPageView = function () {   
    //top layer
    var topLayer = new LinearLayout(this.mContext);
    topLayer.setLayoutParams(new LayoutParams(MATCH_PARENT, 44));
    this.mainNavView = new MainNavView(this.mContext,this.mRouter);    
    topLayer.addChildView(this.mainNavView.view());  
    this.mRootViewObject.addChildView(topLayer.view());
    //center container layer
    var containerLayer = new LinearLayout(this.mContext);  
    containerLayer.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT));
    containerLayer.setPositionStyle("fixed"); 
    this.menuLayer = new FrameLayout(this.mContext,1);
    this.dataLayer = new LinearLayout(this.mContext);       
    //add to container     
    this.updateFrameStyle(false,false);
    containerLayer.addChildView(this.dataLayer.view());
    containerLayer.addChildView(this.menuLayer.view());
    this.mRootViewObject.addChildView(containerLayer.view());
    //update MainNavView
    this.mainNavView.updateMainNavView();
    //end
};
MainPage.prototype.updateFrameStyle = function (isFolded,isFullScreen) {   
    var leftWidth = isFolded?34:180;
    if(isFullScreen){
        leftWidth = 0;
    }
    //dataLayer  
    var parms = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
    parms.mLeftMargin = leftWidth;
    this.dataLayer.setLayoutParams(parms);
    //menuLayer
    var fparms = new LayoutParams(leftWidth, MATCH_PARENT); 
    this.menuLayer.setLayoutParams(fparms);
};
MainPage.prototype.updateNavMenu = function () {
    var leftNavView = new LeftNavView(this.mContext,this.mRouter,this);
    leftNavView.onMenuViewFold = function(isFolded,thePage){ thePage.updateFrameStyle(isFolded);};
    leftNavView.onMenuEvent = function(menuItem,thePage){thePage.updateDataPage(menuItem);};
    this.menuLayer.setView(leftNavView.view());
    this.updateFrameStyle(leftNavView.isFolded(),leftNavView.isFullScreen());

};
MainPage.prototype.updateDataPage = function (menuInfo) {
    this.mRouter.closeDialog();
    this.currentPageFrame = this.buildPageFrame(menuInfo);
    this.dataLayer.setView(this.currentPageFrame.view());
};
MainPage.prototype.buildPageFrame = function (menuInfo) {
    var framePage = this.pageFrames.findFirstByName(menuInfo.getName());
    if(framePage){
        return framePage.getView();
    }    
    var dframe = new FragmentView(this.mContext);   
    dframe.setLayoutParams(new LayoutParams(MATCH_PARENT, MATCH_PARENT));
    dframe.setSrc(this.mContext.mAppConfig.getChildDataPageUrl(menuInfo));    
    framePage = new Word(dframe.getId(), menuInfo.getName(), this.mContext.mAppConfig.getChildDataPageUrl(menuInfo), menuInfo); 
    this.pageFrames.addWord(framePage.setView(dframe));
    return dframe;
};
 
markscript("MainPage");
//end MainPage.js
